package com.example.aslongastoday.db.dao;

import android.content.Context;

import com.example.aslongastoday.domain.BackLog;
import com.example.aslongastoday.domain.BackLogGroup;

import java.util.ArrayList;
import java.util.List;

public class BackLogDao {
    private static volatile BackLogDao dao = null;
    private BackLogGroupDao backLogGroupDao;


    public BackLogDao(Context context) {
        backLogGroupDao = BackLogGroupDao.getInstance(context);
    }

    //单例模式
    public static BackLogDao getInstance(Context context){
        if(dao == null){
            synchronized (BackLogGroupDao.class){
                if(dao == null){
                    dao = new BackLogDao(context);
                }
            }
        }
        return dao;
    }

    /**
     * 回收待办项
     * @param backLogGroupId 代办组id
     * @param position 待办项的下标
     */
    public void recycle(Integer backLogGroupId, Integer position){
        BackLogGroup backLogGroup = backLogGroupDao.query(backLogGroupId);

        List<BackLog> backLogList = backLogGroup.getBackLogList();
//        backLogList.remove(backLogList.get(position));
        backLogList.get(position).setStatus(1);
        backLogGroupDao.update(backLogGroup);
    }

    /**
     * 删除待办项
     * @param backLogGroupId 代办组id
     * @param position 待办项的下标
     */
    public void delete(Integer backLogGroupId, Integer position){
        BackLogGroup backLogGroup = backLogGroupDao.query(backLogGroupId);

        List<BackLog> backLogList = backLogGroup.getBackLogList();
//        backLogList.remove(backLogList.get(position));
        backLogList.get(position).setStatus(2);
        backLogGroupDao.update(backLogGroup);
    }

    public void update(Integer backLogGroupId,Integer position,BackLog backLog){
        BackLogGroup backLogGroup = backLogGroupDao.query(backLogGroupId);

        List<BackLog> backLogList = backLogGroup.getBackLogList();
        BackLog log = backLogList.get(position);
        if (backLog.getStatus()!=null){
            log.setStatus(backLog.getStatus());
        }
        if(backLog.getContent()!=null){
            log.setContent(backLog.getContent());
        }
        if(backLog.getDDL()!=null){
            log.setDDL(backLog.getDDL());
        }
        if(backLog.getTopic()!=null){
            log.setTopic(backLog.getTopic());
        }
        backLogGroupDao.update(backLogGroup);
    }

    /**
     * 查询待办项
     * @param backLogGroupId 代办组的ID
     * @param position 待办项的下标
     * @return
     */
    public BackLog query(Integer backLogGroupId, Integer position){
        BackLogGroup backLogGroup = backLogGroupDao.query(backLogGroupId);

        List<BackLog> backLogList = backLogGroup.getBackLogList();
        return backLogList.get(position);
    }

    /**
     * 主要是为了查询垃圾桶中的
     * @param status -1查询全部 0 正常 1完成 2垃圾桶
     * @return
     */

    public List<BackLog> queryAll(Integer status){
        List<BackLogGroup> backLogGroupList = backLogGroupDao.queryFinishedAll();
        ArrayList<BackLog> list = new ArrayList<>();
        for (BackLogGroup backLogGroup : backLogGroupList) {
            List<BackLog> backLogList = backLogGroup.getBackLogList();
            if(backLogList!=null){
                for (BackLog backLog : backLogList) {
                    Integer aStatus = backLog.getStatus();
                    if(status==-1){
                        list.add(backLog);
                    }else {
                        if (status.equals(aStatus)) {
                            list.add(backLog);
                        }
                    }
                }
            }

        }
        return list;
    }


    public void insert(Integer backLogGroupId,BackLog backLog){
        BackLogGroup backLogGroup = backLogGroupDao.query(backLogGroupId);
        backLog.setStatus(0);
        List<BackLog> backLogList = backLogGroup.getBackLogList();
        if(backLogList==null){
            backLogList = new ArrayList<>();
        }
        backLogList.add(backLog);
        backLogGroupDao.update(backLogGroup);
    }
}
